package com.tianlu.store.controller;
 
import com.tianlu.store.dto.StatusCountDTO;
import com.tianlu.store.entity.OrderInfo;
import com.tianlu.store.enums.OrderStatusEnum;
import com.tianlu.store.service.OrderInfoService;
import com.tianlu.store.utils.JwtUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import java.util.Objects;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import com.tianlu.store.common.PageResult;
import com.tianlu.store.common.ResponseResult;
import com.tianlu.store.common.ResultCode;
import com.tianlu.store.common.IdGen;
 
/**
 * @Author 梁东烂
 * @Desc 订单表(OrderInfo)表控制层
 * @Date 2023-04-03 17:06:04
 */
@Api(tags = "订单表")
@RestController
@RequestMapping("/api/orderInfo")
@RequiredArgsConstructor
public class OrderInfoController {
	@Resource
    private OrderInfoService orderInfoService;
	
	@ApiOperation("订单表列表(分页）接口")
    @PostMapping("/findPage")
    public ResponseResult<PageResult<OrderInfo>> findPage(@RequestBody OrderInfo orderInfo) {
        return ResponseResult.success(orderInfoService.findPage(orderInfo));
    }
	
	@ApiOperation("订单表列表(不分页）接口")
    @PostMapping("/findList")
    public ResponseResult<List<OrderInfo>> findList(@RequestBody OrderInfo orderInfo) {
        return ResponseResult.success(orderInfoService.findList(orderInfo));
    }

    @ApiOperation("查询当前用户的订单表列表(不分页）接口")
    @PostMapping("/findListByUser")
    public ResponseResult<List<OrderInfo>> findListByUser(@RequestBody OrderInfo orderInfo) {
        orderInfo.setUserId(JwtUtils.getIdByJwtToken());
        orderInfo.setDeleteFlag(0);
        return ResponseResult.success(orderInfoService.findList(orderInfo));
    }
	
	@ApiOperation("查看订单表接口")
    @GetMapping(value = "/detail")
    public ResponseResult<OrderInfo> detail(@RequestParam Long id) {
        return ResponseResult.success(orderInfoService.getById(id));
    }
	
	@ApiOperation("添加订单表接口")
    @PostMapping("/save")
    public ResponseResult<Long> save(@RequestBody @Valid OrderInfo orderInfo) {
        orderInfo.setId(IdGen.snowflakeId());
        orderInfo.setUserId(JwtUtils.getIdByJwtToken());
        orderInfoService.save(orderInfo);
        return ResponseResult.success(orderInfo.getId());
    }

    @ApiOperation("添加秒杀/免费订单表接口")
    @PostMapping("/saveKillOrFree")
    public ResponseResult<Long> saveKillOrFree(@RequestBody @Valid OrderInfo orderInfo) {
        orderInfo.setId(IdGen.snowflakeId());
        orderInfo.setUserId(JwtUtils.getIdByJwtToken());
        orderInfoService.saveKillOrFree(orderInfo);
        return ResponseResult.success(orderInfo.getId());
    }
	
	@ApiOperation("修改订单表接口")
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody OrderInfo orderInfo) {
        if (Objects.isNull(orderInfo.getId())) {
            return ResponseResult.fail(ResultCode.CODE_400, "ID不能为空");
        }
        return ResponseResult.success(orderInfoService.updateById(orderInfo));
    }
	
	@ApiOperation("删除订单表接口")
    @PostMapping("/deleteById")
    public ResponseResult<Boolean> deleteById(@RequestParam Long id) {
        orderInfoService.deleteById(id);
        return ResponseResult.success(true);
    }

    @ApiOperation("发货接口")
    @PostMapping("/sendOut")
    public ResponseResult<Boolean> sendOut(@RequestParam Long id) {
        return ResponseResult.success(orderInfoService.updateStatus(id, OrderStatusEnum.WAIT_TAKE.value));
    }

    @ApiOperation("申请退款接口")
    @PostMapping("/drawbackApplication")
    public ResponseResult<Boolean> drawbackApplication(@RequestParam Long id) {
        return ResponseResult.success(orderInfoService.drawbackApplication(id));
    }

    @ApiOperation("确认收货接口")
    @PostMapping("/confirm")
    public ResponseResult<Boolean> confirm(@RequestParam Long id) {
        orderInfoService.confirm(id);
        return ResponseResult.success(true);
    }

    @ApiOperation("退款接口")
    @PostMapping("/drawback")
    public ResponseResult<Boolean> drawback(@RequestParam Long id) {
        return ResponseResult.success(orderInfoService.drawback(id));
    }

    @ApiOperation("取消退款接口")
    @PostMapping("/cancel")
    public ResponseResult<Boolean> cancel(@RequestParam Long id) {
        orderInfoService.cancel(id);
        return ResponseResult.success(true);
    }

    @ApiOperation("统计数量接口")
    @PostMapping("/countByStatus")
    public ResponseResult<List<StatusCountDTO>> countByStatus() {
        Long userId = JwtUtils.getIdByJwtToken();
        return ResponseResult.success(orderInfoService.countByStatus(userId));
    }
}
